{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e82937f7-6b57-4036-aa0f-c249fd227ab4",
   "metadata": {},
   "source": [
    "# zero Dirichlet boundary, no hole, constant source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c41d0310-9839-4bac-aa66-2bde7f7f7085",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Info    : GMSH -> Python-module\n",
      "size: 512 x 512\n",
      "<visvis.wibjects.colorWibjects.Colorbar object at 0x00000143E1C198B0>\n",
      "ERROR calling '_OnAxesPositionChange':\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\wibjects\\colorWibjects.py\", line 622, in _OnAxesPositionChange\n",
      "    self.position.x = axes.position.width+5\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\core\\misc.py\", line 217, in fsetWithDraw\n",
      "    fset(self, *args)\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\core\\base.py\", line 1126, in fset\n",
      "    self._Update()\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\core\\base.py\", line 928, in _Update\n",
      "    self._CalculateInPixels()\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\core\\base.py\", line 997, in _CalculateInPixels\n",
      "    raise Exception(\"Can only calculate the position in pixels\"+\n",
      "Exception: Can only calculate the position in pixels if the owner has a parent!\n",
      "\n",
      "<visvis.wibjects.colorWibjects.Colorbar object at 0x00000143BAF77B50>\n",
      "ERROR calling '_OnAxesPositionChange':\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\wibjects\\colorWibjects.py\", line 622, in _OnAxesPositionChange\n",
      "    self.position.x = axes.position.width+5\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\core\\misc.py\", line 217, in fsetWithDraw\n",
      "    fset(self, *args)\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\core\\base.py\", line 1126, in fset\n",
      "    self._Update()\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\core\\base.py\", line 928, in _Update\n",
      "    self._CalculateInPixels()\n",
      "  File \"D:\\20220326(EFEM-Poisson)\\EFEM\\EFEM\\lib\\site-packages\\visvis\\core\\base.py\", line 997, in _CalculateInPixels\n",
      "    raise Exception(\"Can only calculate the position in pixels\"+\n",
      "Exception: Can only calculate the position in pixels if the owner has a parent!\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import calfem.geometry as cfg\n",
    "import calfem.vis as cfv\n",
    "\n",
    "def create_geometry(el_size=0.2):\n",
    "    g = cfg.Geometry()\n",
    "\n",
    "    points = [[0.0, 0.0],[1.0, 0.0],[1.0, 1.0],[0.0, 1.0]]\n",
    "    splines = [[0, 1],[1, 2],[2, 3],[3, 0]]\n",
    "    surfaces = [[0,1,2,3]]\n",
    "    \n",
    "    for x, y in points:\n",
    "        g.point([x, y], el_size=el_size)\n",
    "\n",
    "    for s in splines:\n",
    "        g.spline(s)\n",
    "\n",
    "    for f in surfaces:\n",
    "        g.surf(f)\n",
    "        # g.struct_surf(f)\n",
    "\n",
    "\n",
    "    bcs = {100:[0],\n",
    "           200:[1],\n",
    "           300:[2],\n",
    "           400:[3],\n",
    "           }    \n",
    "    \n",
    "    for marker in bcs:\n",
    "        for i in bcs[marker]:\n",
    "            g.curve_marker(ID=i, marker=marker)    \n",
    "    \n",
    "    # cfv.drawGeometry(g)\n",
    "    # cfv.showAndWait()\n",
    "    \n",
    "    return g\n",
    "\n",
    "def dir100(x,y):\n",
    "    return np.zeros_like(x)\n",
    "\n",
    "def dir200(x,y):\n",
    "    return np.zeros_like(x)\n",
    "\n",
    "def dir300(x,y):\n",
    "    return np.zeros_like(x)\n",
    "\n",
    "def dir400(x,y):\n",
    "    return np.zeros_like(x)\n",
    "\n",
    "def source(x,y):\n",
    "    return np.ones_like(x)\n",
    "\n",
    "def fk(k,x,y):\n",
    "    return np.sin(k*np.pi*(1+x)/2)*(np.sinh(k*np.pi*(1+y)/2)+np.sinh(k*np.pi*(1-y)/2))/k**3*np.sinh(k*np.pi)\n",
    "\n",
    "def exact_u(n):\n",
    "    def func(x, y):\n",
    "        ret = (1-x**2)/2\n",
    "        for k in range(1, n+1, 2):\n",
    "            ret -= (16/np.pi**3)*fk(k,x,y)\n",
    "        return ret\n",
    "    return func\n",
    "\n",
    "# def exact_ux(x,y):\n",
    "#     return 1-2*x\n",
    "\n",
    "# def exact_uy(x,y):\n",
    "#     return 1-2*y\n",
    "\n",
    "from fem import Poisson2D, ConvergenceRate\n",
    "\n",
    "\n",
    "geometry = create_geometry(el_size=0.05)\n",
    "Dirichlet = [100,200,300,400]\n",
    "Neumann = []\n",
    "hole = []\n",
    "BCfunc = {100:dir100, 200:dir200, 300:dir300, 400:dir400}\n",
    "\n",
    "model = Poisson2D(geometry, Dirichlet, Neumann, hole, BCfunc, source)\n",
    "model.mesh()\n",
    "# model.efem(display=True)\n",
    "# model.display_exact(exact_uy)\n",
    "# print(model.error_efem(exact_ux, exact_uy))\n",
    "model.fem(display=True)\n",
    "model.display_exact(exact_u(21))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c78ef03-8465-4ee3-8e57-16075c50c0bb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "EFEM",
   "language": "python",
   "name": "efem"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
